<?php

// Used this pear package - http://pear.php.net/package/HTTP_Request2/download

/*
Parameters


l - Location around which the deals are searched for. This is a required field. This can be text value or lat/lon value. Examples: Seattle, WA or +43.324-123.432.


q - Query keyword. Optional. For example if you want to find pizza deals, you send keyword as your query 'q=pizza'. If you want to get all deals, either drop q or send q=*


d - Distance around the location specified in the query. Optional. Default value is 5 miles. Always expressed in miles.


si - Start index of the deals. Used for paging through the results. Optional. Default value is 0 - which means start from the beginning.


ps - Page size. Used for paging through the deal results. Optional. Default page size is 20. Maximum number of deals per page is 100.


a - Center'd Activity. Used to filter the results based on the type of activity. For example, send 1 for kid-friendly deals and 8 for romantic deals etc.


c - Deal capability. Used to filter the results based on the deal type. For example, to get daily deals only, send c=16.

ed - Expiration date. Used to filter the deals based on their expiration date. For example to get deals that are expiring soon (say by April 22nd 2010), you send ed=2010-04-22

Example

To get all pizza deals at kid-friendly restaurants in Seattle, you send the request to

http://api.thedealmap.com/search/deals/?l=seattle&q=pizza&a=1
http://api.thedealmap.com/search/deals/?l=seattle&a=1

 */

//Required because the object is a deal object template 
include 'deal.php';
 
require_once "./RESTclient.php";

$rest = new RESTclient();

$query = '';
$distance = 5;
$index = 0;
$pagesize = 20;

//Centered Activities Associative Array
/*
	   Kids = 1
       Group = 8
       Romantic = 16
       Casual = 64
       Fun = 512
       Late-Night = 16384
       Outdoor = 32768
*/

$centeredactivities = array("Kids"=>1, "Group"=>8, "Romantic"=>16, "Casual"=>64, "Fun"=> 512, "Late-Night"=>16384, "Outdoor"=>32768);

// Deal Capability Associative Array

/*
 Favorite = 1 (Favorited by The Dealmap Deal Editors)
       HasTransaction = 2 (Involves some kind of purchase)
       Featured = 4 (This deal is featured on The Dealmap)
       Exclusive = 8 (This deal is exclusively available only at The Dealmap)
       FiftyPercentOrMore = 16 (This deal has 50  or more % savings)
       CanBePrinted = 32 (This is a printable coupon)
 
 */

$dealcapabilities = array("Favorite"=>1, "Transactional"=>2, "Featured"=>4, "Exclusive"=>8, "HalfOff"=> 16, "Printable"=>32);






// Main method in the Query api. Returns an array of "Deal objects".
// Sample execution --  finddealmapdeals('seattle', null,null, null, 65, "Kids", null, null);
findalldealmapdeals('seattle', null,null, null, 65, null, null);


function finddealmapdeals($location, $query, $distance, $index, $pagesize, $centeredactivity, $dealcapability, $expirationdate )
{
	if(!isset($location))
	{
		echo "<Error>Location around which the deals are searched for. This is a required field. This can be text value or lat/lon value. Examples: Seattle, WA or +43.324-123.432. </Error>";
		return;
	}
	if ($pagesize>100)
	{
		echo "<Error>Page size cannot be greater than 100! Please correct your values!!!</Error>";
		return;
	}
	$constructedQuery = constructQuery($location, $query, $distance, $index, $pagesize, $centeredactivity, $dealcapability, $expirationdate);
	//echo $constructedQuery;
	postquery($constructedQuery);
	return convertdealstodealobjects(postquery($constructedQuery));
	
}



// Main method in the Query api. Returns an array of "Deal objects" in case you want to ignore centered categorisation.
// Sample execution --  findalldealmapdeals('seattle', null,null, null, 65,  null, null);

function findalldealmapdeals($location, $query, $distance, $index, $pagesize, $dealcapability, $expirationdate)
{
global $centeredactivities;
	$fulldealarray = array();
	foreach($centeredactivities as $activity=> $activityvalue)
	{
		$dealarrayforthisactivity = finddealmapdeals($location, $query, $distance, $index, $pagesize, $activity, $dealcapability, $expirationdate );
		$fulldealarray = array_merge($dealarrayforthisactivity, $fulldealarray);
		//echo "Went through this for activity ".$activity. " and the value is ".$activityvalue ;
	}
	//print_r($fulldealarray);
	return $fulldealarray;
}







function constructQuery($location, $query, $distance, $index, $pagesize, $centeredactivity, $dealcapability, $expirationdate )
{
global $centeredactivities, $dealcapabilities;
$url = "http://api.thedealmap.com/search/deals/?";
$url = $url."l=".$location;
if(isset($query))
{
	$url = $url."&q=".$query;
}
if(isset($distance) && ($distance!=5) ) 	
{
	$url = $url."&d=".$distance;
}
if(isset($index) && ($index!=0) ) 	
{
	$url = $url."&si=".$index;
}
$url = $url."&ps=".$pagesize;
if(isset($centeredactivity)) 	
{
	$url = $url."&a=".$centeredactivities[$centeredactivity];
}
if(isset($dealcapability)) 	
{
	$url = $url."&c=".$dealcapabilities[$dealcapability];
}
if(isset($expirationdate)) 	
{
	$url = $url."&ed=".$expirationdate;
}

return $url;

}

function postQuery($finalQuery)
{
global $rest;
//$rest = new RESTclient();
header("Content-Type: text/xml");
//$rest->createRequest("$url","POST",$inputs);
$rest->createRequest("$finalQuery","GET");
$rest->sendRequest();
$output = $rest->getResponse();
$sxe=new SimpleXMLElement($output);

//echo $sxe->asXML();
//return $sxe->asXML();
return $sxe;


}

function convertdealstodealobjects($dealsxml)
{
//echo $dealsxml->asXML();

$dealarray = array();
foreach ($dealsxml->Results->Deal as $xmldeal) {
//echo "I wash ere";
	//echo $xmldeal->BusinessID;
   $newdeal = new deal($xmldeal->Activity,$xmldeal->AddedBy,$xmldeal->AdditionalDiscountCouponCode,$xmldeal->AdditionalDiscountCouponEffectiveTime,$xmldeal->AdditionalDiscountCouponExpirationTime,$xmldeal->AdditionalDiscountedValue,$xmldeal->AddressLine,$xmldeal->Affiliation,$xmldeal->BDescription,$xmldeal->BusinessID,$xmldeal->BusinessName,$xmldeal->Capability,$xmldeal->Category,$xmldeal->City,$xmldeal->Country,$xmldeal->Currency,$xmldeal->DealSource,$xmldeal->DealType,$xmldeal->DealUnit,$xmldeal->Description,$xmldeal->DiscountedValue,$xmldeal->EffectiveTime,$xmldeal->ExpirationTime,$xmldeal->FaceValue,$xmldeal->ID,$xmldeal->IconUrl,$xmldeal->ImageUrl,$xmldeal->Keywords,$xmldeal->Latitude,$xmldeal->Longitude,$xmldeal->MoreInfoLink,$xmldeal->Phone,$xmldeal->State,$xmldeal->Tags,$xmldeal->Terms,$xmldeal->Title,$xmldeal->TrackingUrl,$xmldeal->TransactionUrl,$xmldeal->ZipCode,$xmldeal->youSave);	
   array_push($dealarray, $newdeal);
//   echo "Size of the array is ".count($dealarray);
}
return $dealarray;


}

?>